package com.ren.weibo.base.exception;


import com.ren.weibo.base.response.JsonResult;
import com.ren.weibo.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException ex){
        String data=ex.getMessage().split(":")[1].trim();
        log.error("ConstraintViolationException{}",data);
        return new JsonResult(StatusCode.VALIDATE_FAILED,data);

    }

    @ExceptionHandler
    public JsonResult doHandlerMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String data=ex.getFieldError().getDefaultMessage();
        log.error("MethodArgumentNotValidException{}",data);
        return new JsonResult(StatusCode.VALIDATE_FAILED,data);

    }
}
